﻿package mortal.game.scene3D.player.entity
{
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.geom.*;
    import mortal.game.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.model.data.*;

    public class MovePlayer extends SpritePlayer
    {
        protected var _pathArray:Array;
        protected var _needFollowFindPath:Boolean = true;
        protected var _nextPoint:AstarTurnPoint;
        protected var _nextTwoPoint:AstarTurnPoint;
        protected var _isMove:Boolean = false;
        protected var _isInSafeArea:Boolean = false;
        protected var _isInSwinArea:Boolean = false;
        public var nextStartPointHandler:Function;
        private var _followList:Array;
        protected var _currentFollowTarget:MovePlayer;
        private var _collecting:Boolean;

        public function MovePlayer()
        {
            this._pathArray = new Array();
            this._nextPoint = new AstarTurnPoint();
            this._needFollowFindPath = true;
            this._followList = [];
            return;
        }// end function

        override public function updateInfo(param1:Object, param2:Boolean = true) : void
        {
            super.updateInfo(param1, param2);
            this.checkArea();
            this.updateCurrentAlpha();
            return;
        }// end function

        public function get isMove() : Boolean
        {
            return this._isMove;
        }// end function

        override protected function onOnesComplete() : void
        {
            if (!_bodyPlayer)
            {
                return;
            }
            if (_actionType == ActionType.attack || _actionType == ActionType.Injury || _actionType == ActionType.Jump || _actionType == ActionType.Speical)
            {
                if (this.isMove)
                {
                    setAction(ActionType.Walking, ActionName.Walking);
                }
                else
                {
                    setAction(ActionType.Stand, ActionName.Stand);
                }
            }
            return;
        }// end function

        public function get speed() : Speed
        {
            return _speed;
        }// end function

        private function traceUser(param1:String) : void
        {
            if (this is UserPlayer && this is RolePlayer == false)
            {
                Log.debug(entityInfo.entityInfo.name + ":" + param1);
            }
            return;
        }// end function

        public function getTilePoint() : Point
        {
            return GameMapUtil.getTilePoint(this.x2d, this.y2d);
        }// end function

        public function updatePos() : void
        {
            if (this._isMove)
            {
                if (this.isArrive())
                {
                    if (this._pathArray.length > 0 || this._nextTwoPoint != null)
                    {
                        this.walkGridEnd();
                        this.nextPathPoint();
                    }
                    else
                    {
                        this.walkEnd();
                    }
                }
                else
                {
                    this.onWalk();
                    this.onMove();
                    _speed.update(this.x2d, this.y2d);
                }
            }
            return;
        }// end function

        protected function hasNextPoint() : Boolean
        {
            while (this._pathArray.length > 0)
            {
                
                this._nextPoint = this._pathArray.shift() as AstarTurnPoint;
                if (this._nextPoint._x == this.x2d && this._nextPoint._y == this.y2d)
                {
                    continue;
                }
                return true;
            }
            return false;
        }// end function

        protected function nextPathPoint(param1:Boolean = false) : void
        {
            var _loc_2:Point = null;
            if (this.hasNextPoint())
            {
                if (this._nextPoint == null)
                {
                    this.walkEnd();
                    return;
                }
                _loc_2 = GameMapUtil.getTilePoint(this._nextPoint._x, this._nextPoint._y);
                _speed.setPoint(this.x2d, this.y2d, this._nextPoint._x, this._nextPoint._y);
                this._nextPoint.value = getPointValue(_loc_2.x, _loc_2.y);
                updateXray(_loc_2.x, _loc_2.y);
                updateAlpha(this._nextPoint.value);
                this.direction = _speed.direction;
                if (param1)
                {
                    this.walkStart(this._nextPoint);
                }
                else
                {
                    this.walkGridStart(this._nextPoint);
                }
            }
            else
            {
                this.walkEnd();
            }
            return;
        }// end function

        public function updateCurrentAlpha() : void
        {
            var _loc_1:* = GameMapUtil.getTilePoint(this.x2d, this.y2d);
            updateAlpha(getPointValue(_loc_1.x, _loc_1.y));
            return;
        }// end function

        protected function isArrive() : Boolean
        {
            return this.speed.distance < this.speed.speed;
        }// end function

        protected function get speedCorrectPer() : Number
        {
            if (this._pathArray.length > 2)
            {
                return 1.5;
            }
            if (this._pathArray.length > 1)
            {
                return 1.2;
            }
            return 1;
        }// end function

        protected function onMove() : void
        {
            this.x2d = this.x2d + _speed.xSpeed * this.speedCorrectPer;
            this.y2d = this.y2d + _speed.ySpeed * this.speedCorrectPer;
            updateTalkXY();
            if (SceneRange.map.contains(this.x2d, this.y2d) == false)
            {
                this.stopMove();
            }
            ThingUtil.isEntitySort = true;
            return;
        }// end function

        override public function walking(param1:Array) : void
        {
            if (isInitInfo == false || this.isDead)
            {
                this.stopMove();
                return;
            }
            if (param1.length == 0)
            {
                return;
            }
            if (this._isMove == false)
            {
                this._pathArray = param1.concat();
                this.startMove();
                this.nextPathPoint(true);
            }
            else
            {
                this._pathArray = this._pathArray.concat(param1);
            }
            return;
        }// end function

        override public function diversion(param1:Array) : void
        {
            this.checkArea();
            if (isInitInfo == false || this.isDead)
            {
                this.stopMove();
                return;
            }
            if (this._isMove == false)
            {
                this._pathArray = param1.concat();
                this.startMove();
                this.nextPathPoint(true);
            }
            else if (this._pathArray.length)
            {
                this._pathArray.pop();
                if (this._pathArray.length)
                {
                    this._pathArray.pop();
                    this._pathArray = this._pathArray.concat(param1);
                }
                else
                {
                    this._pathArray = this._pathArray.concat(param1);
                    this.nextPathPoint();
                }
            }
            else
            {
                this._pathArray = this._pathArray.concat(param1);
                this.nextPathPoint();
            }
            return;
        }// end function

        public function stopWalking(param1:Boolean = true) : void
        {
            this.stopMove();
            return;
        }// end function

        protected function startMove() : void
        {
            this._isMove = true;
            move();
            return;
        }// end function

        public function stopMove() : void
        {
            if (this._isMove)
            {
                this._isMove = false;
                Global.instance.callLater(this.setActionStand);
            }
            this._pathArray.length = 0;
            this._nextPoint = null;
            this._nextTwoPoint = null;
            return;
        }// end function

        public function setActionStand() : void
        {
            if (!this._isMove && this.isDead == false && ActionType.isWalkAction(this._actionName))
            {
                this.setAction(ActionType.Stand, ActionName.Stand);
            }
            return;
        }// end function

        protected function walkStart(param1:AstarTurnPoint) : void
        {
            if (this.nextStartPointHandler != null)
            {
                this.nextStartPointHandler.call(null, param1);
            }
            return;
        }// end function

        protected function onWalk() : void
        {
            ThingUtil.isEntitySort = true;
            return;
        }// end function

        protected function walkGridStart(param1:AstarTurnPoint) : void
        {
            if (this.nextStartPointHandler != null)
            {
                this.nextStartPointHandler.call(null, param1);
            }
            return;
        }// end function

        protected function walkGridEnd() : void
        {
            if (this._nextPoint)
            {
                _speed.setSpeedXY(this._nextPoint._x - this.x2d, this._nextPoint._y - this.y2d);
                this.x2d = this._nextPoint._x;
                this.y2d = this._nextPoint._y;
                updateTalkXY();
            }
            this.checkArea();
            this.checkDistance();
            if (pointChangeHandler is Function)
            {
                pointChangeHandler();
            }
            return;
        }// end function

        protected function updateDif(param1:int, param2:int) : void
        {
            return;
        }// end function

        public function checkArea() : void
        {
            var _loc_1:* = this.checkIsInSafeArea();
            if (_loc_1 != this._isInSafeArea)
            {
                this._isInSafeArea = _loc_1;
                this.updateIsInSafeArea();
            }
            var _loc_2:* = this.checkIsInSwinArea();
            if (_loc_2 != this._isInSwinArea)
            {
                this._isInSwinArea = _loc_2;
                this.updateIsInSwinArea();
                if (_loc_2)
                {
                    this.enterSwinArea();
                }
                else
                {
                    this.leaveSwinArea();
                }
            }
            return;
        }// end function

        override public function updateCollect(param1:Boolean) : void
        {
            if (param1 && this._isInSwinArea)
            {
                setAction(ActionType.leading, ActionName.SwimStand);
            }
            else
            {
                super.updateCollect(param1);
            }
            return;
        }// end function

        override protected function checkDistance() : void
        {
            if (entityInfo)
            {
                entityInfo.updateDistance(toRoleDistance);
            }
            return;
        }// end function

        protected function checkIsInSafeArea() : Boolean
        {
            var _loc_1:* = GameMapUtil.getTilePoint(this.x2d, this.y2d);
            var _loc_2:* = getPointValue(_loc_1.x, _loc_1.y);
            var _loc_3:* = MapNodeType.isSameServerSafe(_loc_2);
            return _loc_3;
        }// end function

        protected function checkIsInSwinArea() : Boolean
        {
            var _loc_1:* = GameMapUtil.getTilePoint(this.x2d, this.y2d);
            var _loc_2:* = getPointValue(_loc_1.x, _loc_1.y);
            var _loc_3:* = MapNodeType.isSwinArea(_loc_2);
            return _loc_3;
        }// end function

        protected function updateIsInSafeArea() : void
        {
            if (entityInfo && entityInfo.entityInfo)
            {
                entityInfo.isUpdateName = true;
                entityInfo.isUpdateLife = true;
            }
            return;
        }// end function

        protected function updateIsInSwinArea() : void
        {
            if (entityInfo && entityInfo.entityInfo)
            {
                updateActionName();
            }
            return;
        }// end function

        protected function enterSwinArea() : void
        {
            return;
        }// end function

        protected function leaveSwinArea() : void
        {
            return;
        }// end function

        protected function walkEnd() : void
        {
            this.walkGridEnd();
            this.stopMove();
            return;
        }// end function

        public function setViewCenter(param1:Boolean = true, param2:Boolean = true) : void
        {
            var _loc_3:* = SceneRange.moveMap.x;
            var _loc_4:* = SceneRange.moveMap.y;
            var _loc_5:* = SceneRange.display;
            if (SceneRange.map.contains(this.x2d, this.y2d))
            {
                if (param1)
                {
                    _loc_5.x = int(this.x2d - _loc_5.width / 2);
                    if (_loc_5.left < SceneRange.map.left)
                    {
                        _loc_5.x = 0;
                    }
                    if (_loc_5.right > SceneRange.map.right)
                    {
                        _loc_5.x = int(SceneRange.map.right - _loc_5.width);
                    }
                }
                if (param2)
                {
                    _loc_5.y = int(this.y2d - _loc_5.height / 2);
                    if (_loc_5.top < SceneRange.map.top)
                    {
                        _loc_5.y = 0;
                    }
                    if (_loc_5.bottom > SceneRange.map.bottom)
                    {
                        _loc_5.y = int(SceneRange.map.bottom - _loc_5.height);
                    }
                }
                Game.scene.scrollRect = _loc_5;
            }
            else if (Global.isDebugModle)
            {
                Alert.show(Language.getString(20670));
            }
            return;
        }// end function

        public function get pathArray() : Array
        {
            return this._pathArray;
        }// end function

        public function get followList() : Object
        {
            return this._followList;
        }// end function

        public function removeFollow(param1:MovePlayer) : void
        {
            var _loc_4:MovePlayer = null;
            var _loc_2:* = this._followList.length;
            var _loc_3:uint = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = this._followList[_loc_3] as MovePlayer;
                if (_loc_4 == param1)
                {
                    this._followList.splice(_loc_3, 1);
                }
                _loc_3 = _loc_3 + 1;
            }
            return;
        }// end function

        public function addFollow(param1:MovePlayer) : void
        {
            this._followList.push(param1);
            return;
        }// end function

        public function follow(param1:MovePlayer) : void
        {
            return;
        }// end function

        public function get hasMount() : Boolean
        {
            return false;
        }// end function

        public function set currentFollowTarget(param1:MovePlayer) : void
        {
            if (this._currentFollowTarget && this._currentFollowTarget != param1)
            {
                this._currentFollowTarget.removeFollow(this);
            }
            this._currentFollowTarget = param1;
            return;
        }// end function

        public function get currentFollowTarget() : MovePlayer
        {
            return this._currentFollowTarget;
        }// end function

        public function getFollowIndex(param1:MovePlayer) : int
        {
            var _loc_2:int = -1;
            var _loc_3:* = this._followList.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                if (this._followList[_loc_4] as MovePlayer == param1)
                {
                    _loc_2 = _loc_4;
                    return _loc_2;
                }
                _loc_4++;
            }
            return _loc_2;
        }// end function

        override public function set isDead(param1:Boolean) : void
        {
            super.isDead = param1;
            if (param1)
            {
                this.stopMove();
            }
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this.stopMove();
            this._isInSafeArea = false;
            this._isInSwinArea = false;
            this.collecting = false;
            this.nextStartPointHandler = null;
            super.dispose(param1);
            return;
        }// end function

        public function get collecting() : Boolean
        {
            return this._collecting;
        }// end function

        public function set collecting(param1:Boolean) : void
        {
            this._collecting = param1;
            return;
        }// end function

        public function get isInSwinArea() : Boolean
        {
            return this._isInSwinArea;
        }// end function

    }
}
